

package cn.shengchao.examstar.distribute.ohs.remote.rpc;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.rpc.base.TransResult;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeDataValidEnum;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeUserInvite;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeUserInviteService;
import cn.shengchao.examstar.distribute.ohs.local.appservice.DistributeUserInviteDomainService;
import cn.shengchao.examstar.rpc.distribute.DistributeUserInviteApi;
import cn.shengchao.examstar.rpc.distribute.dto.UserInviteDto;
import cn.shengchao.examstar.rpc.base.exception.BaseRpcException;
import cn.shengchao.examstar.rpc.distribute.req.DistributeJmsYgBatchInviteChangeReq;
import cn.shengchao.examstar.rpc.uddsys.req.DistributeUserInviteSaveDto;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @program: examstarapi
 * @description: 用户受邀的分销记录处理
 * @author: Xiong weiqiang
 * @create: 2025-03-25 11:19
 */
@DubboService
public class DistributeUserInviteDubboApi implements DistributeUserInviteApi {
    @Autowired
    private DistributeUserInviteDomainService distributeUserInviteDomainService;
    @Autowired
    private DistributeUserInviteService distributeUserInviteService;

    /**
     * method : userInviteDistributeRecordIn
     * description : 保存用户的邀请记录，邀请用户不一定是平台有效用户（比如离职），分销用户才是最终服务用户
     * 
     * @param userInviteDto \{@link UserInviteDto \}
     * @author : Xiong weiqiang
     * @CreateDate : 2025-03-25 14:43:17
     *
     * @Reviewed :
     * @UpdateLog : Name Date Reason/Contents
     *            ---------------------------------------
     *            **** **** ****
     *
     */

    @Override
    public void userInviteDistributeRecordIn(UserInviteDto userInviteDto) {
        distributeUserInviteDomainService.userInviteDistributeRecordIn(userInviteDto);
    }

    @Override
    public TransResult<String> updateUserInviteRecord(DistributeUserInviteSaveDto userInviteSaveDto) {
        return distributeUserInviteDomainService.updateUserInviteRecord(userInviteSaveDto);
    }

    @Override
    public TransResult<String> batchUpdateUserInviteRecordForJmsYg(DistributeJmsYgBatchInviteChangeReq jmsYgBatchInviteChangeReq) {
        return distributeUserInviteDomainService.batchUpdateUserInviteRecordForJmsYg(jmsYgBatchInviteChangeReq);
    }

    /**
     * method : getUserInviteRecord
     * description : 查询用户的受邀记录
     *
     * @param userIds \{@link Long \}
     * @return : cn.shengchao.examstar.distribute.rpc.dto.UserInviteDto
     * @author : Xiong weiqiang
     * @CreateDate : 2025-03-25 14:11:57
     *
     * @Reviewed :
     * @UpdateLog : Name Date Reason/Contents
     *            ---------------------------------------
     *            **** **** ****
     *
     */
    @Override
    public List<UserInviteDto> getUserInviteRecordsForUsers(List<Long> userIds) {
        return distributeUserInviteService.selectUserInviteRecords(userIds);
    }
    /**
     * method : getUserInviteRecord
     * description : 查询用户的受邀记录
     * 
     * @param userId \{@link Long \}
     * @return : cn.shengchao.examstar.distribute.rpc.dto.UserInviteDto
     * @author : Xiong weiqiang
     * @CreateDate : 2025-03-25 14:11:57
     * 
     * @Reviewed :
     * @UpdateLog : Name Date Reason/Contents
     *            ---------------------------------------
     *            **** **** ****
     *
     */
    @Override
    public UserInviteDto getUserInviteRecord(Long userId) {
        List<DistributeUserInvite> distributeUserInviteList = distributeUserInviteService.list(Wrappers
            .lambdaQuery(DistributeUserInvite.class)
            .eq(DistributeUserInvite::getUserId, userId)
            .eq(DistributeUserInvite::getDataStatus, DistributeDataValidEnum.STATUS_VALID.getStatus()));
        //如果用户没有邀请记录
        if (CollUtil.isEmpty(distributeUserInviteList)) {
            return null;
        } else if (distributeUserInviteList.size() > 1) {
            throw new BaseRpcException("用户邀请记录大于1");
        } else {
            DistributeUserInvite distributeUserInvite = distributeUserInviteList.getFirst();
            UserInviteDto userInviteDto = BeanUtil.copyProperties(distributeUserInvite, UserInviteDto.class);
            return userInviteDto;
        }
    }


}